package cn.edu.cxcyzx.util;

import java.io.IOException;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import cn.edu.cxcyzx.conf.Git;
import cn.edu.cxcyzx.entity.Gitee;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
 * HttpClient 模拟发送http请求
 * @author zxl
 * @date   2018年6月18日上午9:28:41
 * @描述:
 *
 */
public class HttpClientUtil {
	
	/**
	 * GET请求
	 * @param url
	 * @param params
	 * @return	String
	 */
	public static String doGet(String url,List<NameValuePair> params){
		CloseableHttpClient client=HttpClients.createDefault();
		try {
			String param=EntityUtils.toString(new UrlEncodedFormEntity(params,Consts.UTF_8));
			HttpGet httpGet=new HttpGet(url+"?"+param);
			httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
			HttpResponse response=client.execute(httpGet);
			System.out.println("statusCode:"+response.getStatusLine().getStatusCode());
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				HttpEntity httpEntity=response.getEntity();
				String res=EntityUtils.toString(httpEntity, "utf-8");
				return res;
			}
		} catch (ParseException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
		
	}
	
	/**
	 * POST请求
	 * @param url
	 * @param jsonObject
	 * @return	JSONObject
	 */
	public static JSONObject doPost(String url,JSONObject jsonObject) {
		CloseableHttpClient client=HttpClients.createDefault();
		HttpPost httpPost=new HttpPost(url);
		System.out.println("jsonObject:"+jsonObject.toString());
		StringEntity entity=new StringEntity(jsonObject.toString(), "utf-8");
		entity.setContentEncoding("UTF-8");
		entity.setContentType("application/json");
		//设置User-Agent,否则请求码403
		httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
		httpPost.setEntity(entity);
		HttpResponse httpResponse = null;
		try {
			httpResponse = client.execute(httpPost);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int resultCode=httpResponse.getStatusLine().getStatusCode();
		System.out.println("resultCode:"+resultCode);
		if(resultCode==200) {
			HttpEntity entity2=httpResponse.getEntity();
			try {
				String result=EntityUtils.toString(entity2, "utf-8");
				return JSONObject.parseObject(result);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					client.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		return null;
	}
}
